#!usr/bin/env python  
# -*- coding:utf-8 -*-
""" 
@author:'手机视界' 
@file: site_maps.py 
@time: 2018/12/11

site_map配置页面
"""
import requests
import json

from django.contrib.sitemaps import GenericSitemap
from article_manager.models import Article
from bbd_admin.models import SiteSetting

article_dict = {
    'queryset': Article.objects.filter(delete=False, hide=False),
    'date_field': 'update_time',
}

sitemaps = {
    # 'flatpages': FlatPageSitemap,
    'article': GenericSitemap(article_dict, priority=0.8),
}


def post_urls_to_baidu(data):
    """
    向百度主动推送链接
    data 'url1\nurl2\n.....'
    :return:
    """
    to = ""
    site_setting = SiteSetting.objects.first()
    if site_setting:
        to = site_setting.push_url
    headers = {
        'User - Agent': 'curl / 7.12.1',
        'Host': 'data.zz.baidu.com',
        'Content - Type': 'text / plain',
        'Content - Length': '83',
    }
    resp = requests.post(to, headers=headers, data=data)
    return json.loads(resp.content.decode('utf8'))


# if __name__ == '__main__':
#     post_urls_to_baidu('http://bianbingdang.com/article_detail/104.html')
